package com.doctorliang.cheng.shopping.shoppintcar.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.app.MainActivity;
import com.doctorliang.cheng.shopping.base.BaseFragment;
import com.doctorliang.cheng.shopping.home.bean.GoodsBean;
import com.doctorliang.cheng.shopping.shoppintcar.adapter.ShowShoppingAdapter;
import com.doctorliang.cheng.shopping.shoppintcar.utils.CartStorage;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.content.ContentValues.TAG;

/**
 * Created by CHENG on 2017/9/26.
 * 购物车视图的Fragment
 */

public class ShoopingCarFragment extends BaseFragment {
    //编辑状态
    private static final int ACTION_EDIT = 1;
    //完成状态
    private static final int ACTION_COMPLITE = 2;
    @Bind(R.id.tv_shopcart_edit)
    TextView tvShopcartEdit;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.checkbox_all)
    CheckBox checkboxAll;
    @Bind(R.id.tv_shopcart_total)
    TextView tvShopcartTotal;
    @Bind(R.id.btn_check_out)
    Button btnCheckOut;
    @Bind(R.id.ll_check_all)
    LinearLayout llCheckAll;
    @Bind(R.id.cb_all)
    CheckBox cbAll;
    @Bind(R.id.btn_delete)
    Button btnDelete;
    @Bind(R.id.btn_collection)
    Button btnCollection;
    @Bind(R.id.ll_delete)
    LinearLayout llDelete;
    @Bind(R.id.iv_empty)
    ImageView ivEmpty;
    @Bind(R.id.tv_empty_cart_tobuy)
    TextView tvEmptyCartTobuy;
    private LinearLayout ll_empty;
    private ShowShoppingAdapter adapter;

    @Override
    public View initView() {
        View view = View.inflate(mcontext, R.layout.fragment_shopping_cart, null);
        ll_empty = view.findViewById(R.id.ll_empty);
        Log.e(TAG, "购物车视图被初始化了");
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        showData();////////////////////////////////////////////
    }

    @Override
    public void initData() {
        super.initData();
        Log.e(TAG, "购物车视图的数据被初始化了");
        showData();

    }

    private void showData() {
        //一初始化数据的时候就要加载已保存过的
        List<GoodsBean> goodsBeanList = CartStorage.getInstance().getAllData();
        if (goodsBeanList != null && goodsBeanList.size() > 0) {
            //have datas
            //隐藏空布局
            ll_empty.setVisibility(View.GONE);
            //结算视图显示
            llCheckAll.setVisibility(View.VISIBLE);
            //显示编辑文本
            tvShopcartEdit.setVisibility(View.VISIBLE);

            //设置recycleview的适配器
            adapter = new ShowShoppingAdapter(mcontext,goodsBeanList,checkboxAll,tvShopcartTotal,cbAll);
            recyclerview.setAdapter(adapter);
            //设置布局管理者
            recyclerview.setLayoutManager(new LinearLayoutManager(mcontext,LinearLayoutManager.VERTICAL,false));
        } else {
            //null
            //显示空布局
            showEmptyShoppingCart();
        }
    }

    private void showEmptyShoppingCart() {
        //显示空布局
        ll_empty.setVisibility(View.VISIBLE);
        //隐藏(编辑)文本
        tvShopcartEdit.setVisibility(View.GONE);
        //隐藏（删除）
        llDelete.setVisibility(View.GONE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initListener();
        return rootView;
    }

    private void initListener() {
        //设置默认的编辑状态
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int action = (int) view.getTag();
                if (action == ACTION_EDIT) {
                    //转换为完成状态
                    showComplete();
                } else {
                    //切换为编辑状态
                    showEdit();
                }
            }


        });
        tvEmptyCartTobuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mcontext, MainActivity.class));
            }
        });
    }

    /**
     * 显示编辑状态
     */
    private void showEdit() {
        //1设置状态和文本
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setText("编辑");
        //2变成全勾选
        if (adapter!=null) {
            adapter.check_all_none(true);
            adapter.checkAll();
            adapter.showTotalPrice();
        }
        //3删除视图显示
        llDelete.setVisibility(View.GONE);
        //4结算视图隐藏
        llCheckAll.setVisibility(View.VISIBLE);
    }

    /**
     * 显示完成状态
     */
    private void showComplete() {
        //1设置状态和文本
        tvShopcartEdit.setTag(ACTION_COMPLITE);
        tvShopcartEdit.setText("完成");
        //2变成非勾选
        if (adapter!=null) {
            adapter.check_all_none(false);
            adapter.checkAll();
        }
        //3删除视图显示
        llDelete.setVisibility(View.VISIBLE);
        //4结算视图隐藏
        llCheckAll.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_shopcart_edit, R.id.recyclerview, R.id.checkbox_all, R.id.tv_shopcart_total, R.id.btn_check_out, R.id.ll_check_all, R.id.cb_all, R.id.btn_delete, R.id.btn_collection, R.id.ll_delete, R.id.iv_empty, R.id.tv_empty_cart_tobuy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_shopcart_edit:
//                llDelete.setVisibility(View.VISIBLE);
//                llCheckAll.setVisibility(View.GONE);
                break;
            case R.id.recyclerview:
                break;
            case R.id.checkbox_all:
                break;
            case R.id.tv_shopcart_total:
                break;
            case R.id.btn_check_out:
                break;
            case R.id.ll_check_all:
                break;
            case R.id.cb_all:
                break;
            case R.id.btn_delete:
                //1删除选中的
                adapter.delete();
                //2校验状态
                adapter.checkAll();
                if (adapter.getItemCount()==0) {
                    showEmptyShoppingCart();
                }
                break;
            case R.id.btn_collection:
                break;
            case R.id.ll_delete:
                break;
            case R.id.iv_empty:
                break;
            case R.id.tv_empty_cart_tobuy:
                break;
        }
    }
}
